package com.zrc.module_find.activity.defrecycleview.demo3

import android.annotation.SuppressLint
import android.util.SparseArray
import androidx.recyclerview.widget.RecyclerView
import com.zrc.module_find.activity.defrecycleview.demo3.DemoAdapter.MyHolder
import android.view.ViewGroup
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.blankj.utilcode.util.ToastUtils
import com.zrc.superframe_ui.layout.SuperScrollView
import com.zrc.module_find.R
import com.zrc.module_find.activity.defrecycleview.DemoResponse

@SuppressLint("NewApi")
class DemoAdapter(bindScrollView: SuperScrollView) : RecyclerView.Adapter<MyHolder>() {
    var offestX = 0
    private val views: MutableList<SuperScrollView> = ArrayList()
    var datas= ArrayList<DemoResponse.OtherListBean>()
    @SuppressLint("NewApi")
    var l:View.OnScrollChangeListener = (View.OnScrollChangeListener { v, l, t, oldl, oldt ->
        offestX = l
        views.forEach {
            it.scrollTo(offestX, 0)
        }
    })
    init {
        views.add(bindScrollView)
        bindScrollView.setOnScrollChangeListener(l)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
        val holder= MyHolder(view)
        val scrollView = holder.getView<SuperScrollView>(R.id.myScrollView)
        views.add(scrollView)
        scrollView.setScrollChangeListener(l)
        l.onScrollChange(scrollView,offestX,0,0,0)
        return holder
    }

    override fun onBindViewHolder(holder: MyHolder, position: Int) {
        holder.getView<TextView>(R.id.tv0).text=(position+1).toString()
        holder.getView<TextView>(R.id.tv1).text=datas[position].id.toString()
        holder.getView<TextView>(R.id.tv2).text=datas[position].title
        holder.getView<TextView>(R.id.tv3).text=datas[position].image
        holder.getView<TextView>(R.id.tv4).text=datas[position].tag
        holder.getView<TextView>(R.id.tv5).text=datas[position].author_name
        holder.getView<TextView>(R.id.tv6).text=datas[position].like_num.toString()
        holder.getView<TextView>(R.id.tv7).text=datas[position].create_datetime
        holder.getView<TextView>(R.id.tv8).text=datas[position].abstract
        holder.getView<LinearLayout>(R.id.ll_item).setOnClickListener {
            ToastUtils.showLong(datas[position].author_name)
        }
    }

    override fun getItemCount()=datas.size

    @SuppressLint("NotifyDataSetChanged")
    fun setData(data: ArrayList<DemoResponse.OtherListBean>) {
        this.datas=data
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addData(data: ArrayList<DemoResponse.OtherListBean>) {
        this.datas.addAll(data)
        notifyDataSetChanged()
    }

    inner class MyHolder(itemView: View) : ViewHolder(itemView){
        private val viewSparseArray: SparseArray<View> = SparseArray()
        fun <T : View> getView(viewId: Int): T {
            var view = viewSparseArray[viewId]
            if (view == null) {
                view = itemView.findViewById(viewId)
                viewSparseArray.put(viewId, view)
            }
            return view as T
        }
    }
}